package com.service.impl;

import com.entity.Namespace;
import com.repository.NamespaceRepository;
import com.service.NamespaceService;
import io.fabric8.kubernetes.api.model.NamespaceList;
import io.fabric8.kubernetes.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;


@Service
@Transactional
public class NamespaceServiceImpl implements NamespaceService {
    private static final Logger logger = LoggerFactory.getLogger(com.service.impl.NamespaceServiceImpl.class);

    private NamespaceRepository namespaceRepository;

    @Autowired
    public NamespaceServiceImpl(NamespaceRepository namespaceRepository) {
        this.namespaceRepository = namespaceRepository;
    }

    @Override
    public Page<Namespace> findByPageAndParams(Integer page, Integer size, Namespace namespace, String orderBy,String sortOrder) {
            Integer Page = page - 1;
            String sortorder = sortOrder.toUpperCase();
            Sort sort = new Sort(Sort.Direction.valueOf(sortorder), orderBy);
            Pageable pageable = PageRequest.of(Page, size, sort);
        if ( namespace != null ){
            Specification<Namespace> spec = new Specification<Namespace>() {
                @Override
                public Predicate toPredicate(Root<Namespace> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
//                    Path<Integer> namespaceid = root.get("namespaceid");
                    Path<String> spacename = root.get("spacename");
                    //Predicate p1 = cb.equal(namespaceid,param.getNamespaceid());
                    Predicate p2 = cb.like(spacename, "%" + namespace.getSpacename() + "%");
                    Predicate p = cb.and(p2);
                    return p;
                }};
            return namespaceRepository.findAll(spec, pageable);
        }
        else {
            return namespaceRepository.findAll(pageable);
        }
    };

    @Override
    public List<Namespace> findAll() {
        return namespaceRepository.findAll();
    }

    @Override
    public void deleteNamespace(int id) {
         namespaceRepository.deleteNamespacesById(id);
    }

    @Override
    public List<String> findK8sNamespace(Namespace[] nslist){
        List<String> nsdifflist=new ArrayList<String>();
        Config config = new ConfigBuilder().build();
        try (final KubernetesClient client = new DefaultKubernetesClient(config)) {
            NamespaceList nsl = client.namespaces().list();
            //System.out.print(nsl);
            List<io.fabric8.kubernetes.api.model.Namespace> items = nsl.getItems();
            for (io.fabric8.kubernetes.api.model.Namespace item : items) {
                for (Namespace ns : nslist) {
                    if (ns.getSpacename().equals(item.getMetadata().getName())){
                        logger.info(ns.getSpacename() + "====" + item.getMetadata().getName());
                        nsdifflist.add(item.getMetadata().getName());
                    }
                }
            }
            //logger.info(storageClassList.toString());
        } catch (KubernetesClientException e) {
            logger.error(e.getMessage(), e);
        }
        return nsdifflist;
     }

}
